package com.dance.java8.day01;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestFunction {
    @Test
    public void test4() {
        Consumer<String> consumer = System.out::println;
        consumer.accept("flower");
    }

    @Test
    public void test5() {
        happy("hello world", str -> System.out.println("this is " + str));
    }

    public void happy(String str, Consumer<String> consumer) {
        consumer.accept(str);
    }

    @Test
    public void test6() {
        List<Integer> num = getNum(10, () -> (int) (Math.random() * 100));
        num.forEach(System.out::println);
    }

    public List<Integer> getNum(int num, Supplier<Integer> supplier) {
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            integers.add(supplier.get());
        }
        return integers;
    }

    @Test
    public void test7() {
        String hello_world = stringHandler("hello world", s -> s.substring(0, 5));
        System.out.println(hello_world);
    }

    public String stringHandler(String string, Function<String, String> function) {
        return function.apply(string);
    }

    @Test
    public void test8() {
        boolean query = isQuery("select query", s -> s.equals("select query"));
        System.out.println(query);
    }

    public boolean isQuery(String string, Predicate<String> predicate) {
        return predicate.test(string);
    }
}
